<template>
  <KView class="one-auction">
    <page-header title="一元拍卖" @back-click="toBack"></page-header>
    <KView class="one-auction-line"></KView>
    <KView class="one-auction-content" v-if="isShow">
      <auction-view ref="auctionView" :goods-list="goodsList" :pagination="pagination" :next-data-fun="setAuctionData" @item-click="itemClick"></auction-view>
    </KView>
  </KView>
</template>

<script>
  import SystemUtil from 'utils/systemUtil';

  import PageHeader from 'common/pageHeader';
  import AuctionView from './auctionView';

  import Pagination from 'models/pagination';

  import AuctionApi from 'services/auctionApi';

  export default {
    props: {
      hasBack: {
        type: Boolean,
        default: true
      }
    },

    data() {
      return {
        isShow: false,
        isMiniprogram: process.env.isMiniprogram,
        goodsList: [],
        pagination: new Pagination()
      };
    },

    created() {
      this.initData();
    },

    methods: {
      initData() {
        SystemUtil.showLoading();
        this.setAuctionData(true).then(() => {
          this.isShow = true;
          SystemUtil.hideLoading();
        }).catch(() => {
          SystemUtil.hideLoading();
        });
      },

      setAuctionData(isInit = false, pagination) {
        if (pagination === undefined) {
          pagination = this.pagination;
        }
        return AuctionApi.getAuctionList(pagination).then(goodsList => {
          if (isInit) {
            this.goodsList = goodsList;
          } else {
            this.goodsList.push(...goodsList);
          }
          return goodsList;
        });
      },

      resetData() {
        if (this.$refs['auctionView'].loading) {
          return window.Promise.resolve([]);
        }

        let pagination = new Pagination();
        SystemUtil.showLoading('刷新中...');
        return this.setAuctionData(true, pagination).then(goodsList => {
          this.pagination = pagination;
          SystemUtil.hideLoading();
          return goodsList;
        }).catch(() => {
          SystemUtil.hideLoading();
          return [];
        });
      },

      toBack() {
        if (!this.isMiniprogram) {
          window.history.back();
          return;
        }

        if (this.hasBack) {
          wx.navigateBack();
        } else {
          this.$emit('to-page', '/home');
        }
      },

      itemClick(goods) {
        let defId = goods.defId;
        if (SystemUtil.isEmpty(defId)) {
          return;
        }

        let url = `/auctionDetail/${defId}`;
        if (this.isMiniprogram) {
          window.open(url);
        }
      }
    },

    components: {
      PageHeader,
      AuctionView
    }
  };

</script>

<style lang="scss">
  .one-auction {
    height: 100%;

    .page-header {
      margin-bottom: formatPx(20);
    }

    .one-auction-line {
      height: 1px;
      background: #DADADA;
    }

    .one-auction-content {
      margin-top: formatPx(40);
      height: calc(100% - #{formatPx(135)});
    }
  }

</style>
